﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO.Common;
using DAO.Common;
using System.Data.Common;
using System.Data;
using DTO.Headquarters;
using System.Transactions;
using System.ServiceModel;
using ServiceInterfaces.Stores;

namespace DAO.Headquarter
{
    public class OrderDAO : DAO.Common.DAO
    {

        public OrderDAO(string provider, string connStr): base(provider, connStr) { }

        public List<Product> GetProductsToOrder(int page, int recordsPerPage, OrderStatus orderStatus)
        {
            // default to show all
            bool order1 = true;
            bool order2 = false;
            if(OrderStatus.Ordered == orderStatus)
                order2 = true;
            if(OrderStatus.NotOrdered == orderStatus)
                order1 = false;
            
            List<Product> list = new List<Product>();
            using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
            {
                DbCommand cmd = DBHelper.CreateCommandWithParameters(
                    conn,
                    "productsToOrder",
                    CommandType.StoredProcedure,
                    new List<ParameterHelper> {
                        new ParameterHelper{ DbType = DbType.Int32, Name = "page", Value = page },
                        new ParameterHelper{ DbType = DbType.Int32, Name = "recordsPerPage", Value = recordsPerPage },
                        new ParameterHelper{ DbType = DbType.Boolean, Name = "order1", Value = order1 },
                        new ParameterHelper{ DbType = DbType.Boolean, Name = "order2", Value = order2 }
                    });

                conn.Open();
                using (DbDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Product p = new Product();
                        p.FromDataReader(reader);

                        list.Add(p);
                    }
                }
            }

            return list;
        }

        public int GetNumberOfProductsToOrder(OrderStatus orderStatus)
        {
            // default to show all
            bool order1 = true;
            bool order2 = false;
            if (OrderStatus.Ordered == orderStatus)
                order2 = true;
            if (OrderStatus.NotOrdered == orderStatus)
                order1 = false;

            int nrResults = 0;
            using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
            {
                DbCommand cmd = DBHelper.CreateCommandWithParameters(
                    conn,
                    "totalProductsToOrder",
                    CommandType.StoredProcedure,
                    new List<ParameterHelper> {
                        new ParameterHelper{ DbType = DbType.Boolean, Name = "order1", Value = order1 },
                        new ParameterHelper{ DbType = DbType.Boolean, Name = "order2", Value = order2 }
                    });

                conn.Open();
                using (DbDataReader reader = cmd.ExecuteReader())
                {
                    if(reader.Read())
                        nrResults = reader.GetInt32(0);
                }
            }

            return nrResults;
        }

        public void MarkProductAsOrdered(int productCode)
        {
            using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
            {
                DbCommand command = DBHelper.CreateCommandWithParameters(
                    conn,
                    "productOrdered",
                    CommandType.StoredProcedure,
                    new List<ParameterHelper> { 
                        new ParameterHelper { DbType = DbType.Int32, Name = "productCode", Value = productCode }
                    });

                conn.Open();
                command.ExecuteNonQuery();
            }
        }

        public void UpdateProductStock(int productCode, int amount)
        {

            using (TransactionScope ts = new TransactionScope(
                TransactionScopeOption.Required,
                new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
            {
                string productType = null;
                using (DbConnection conn = DBHelper.CreateDBConnection(Prodiver, ConnectionStr))
                {
                    DbCommand command = DBHelper.CreateCommandWithParameters(
                        conn,
                        "orderReceived",
                        CommandType.StoredProcedure,
                        new List<ParameterHelper> { 
                        new ParameterHelper { DbType = DbType.Int32, Name = "productCode", Value = productCode },
                        new ParameterHelper { DbType = DbType.Int32, Name = "amount", Value = amount } 
                    });

                    conn.Open();
                    //command.ExecuteNonQuery();
                    productType = (string)command.ExecuteScalar();
                }

                ProductType type = (ProductType)Enum.Parse(typeof(ProductType), productType);

                // Add the product to its respective store
                ChannelFactory<IStoresManagement> store = ChannelFactoryStoresHelper.CreateChannelFactoryToStores<IStoresManagement>(Operation.UPDATE, type);
                IStoresManagement storeManagement = store.CreateChannel();
                storeManagement.UpdateProductStock(productCode, amount);
                store.Close();

                ts.Complete();
            }
        }

    }
}
